package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.Wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private CacheService cacheService;

    @Override
    public void computeHotArticle() {
        clearCache();

        Calendar calendar= Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK,-30);
        //1 查询最近5天内新闻
        Date Last5days = calendar.getTime();
        List<ApArticle> apArticles = apArticleMapper.findArticleListByLast5days(Last5days);

        //2 计算分值
        List<HotArticleVo> hotArticleVoAllChanlle = apArticles.stream().map(apArticle -> {
            HotArticleVo vo = new HotArticleVo();
            BeanUtils.copyProperties(apArticle, vo);
            Integer score = calculateScore(apArticle);

            vo.setScore(score);
            return vo;
        }).collect(Collectors.toList());
        //3 按照频道分类,排序
        ResponseResult channelsResponseResult = iWemediaClient.getChannels();
        String channelsListString = JSON.toJSONString(channelsResponseResult.getData());

        List<WmChannel> wmChannels = JSON.parseArray(channelsListString, WmChannel.class);

        for (WmChannel wmChannel : wmChannels) {
            List<HotArticleVo> hotArticleVoBy = hotArticleVoAllChanlle.stream().filter(hotArticleVo -> {
                return hotArticleVo.getChannelId().equals(wmChannel.getId());
            }).collect(Collectors.toList());
            //按照排序
            Collections.sort(hotArticleVoBy, new Comparator<HotArticleVo>() {
                @Override
                public int compare(HotArticleVo o1, HotArticleVo o2) {
                    return o2.getScore()-o1.getScore();
                }
            });

            if (hotArticleVoBy.size()>=30){
                hotArticleVoBy.stream().limit(30);
            }
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+wmChannel.getId(),JSON.toJSONString(hotArticleVoBy));
        }
        //4 按照频道放入缓存
        //5 定时支持
    }

    private void clearCache() {
        Set<String> scan = cacheService.scan(ArticleConstants.HOT_ARTICLE_FIRST_PAGE);
        cacheService.delete(scan);
    }

    private Integer calculateScore(ApArticle apArticle) {
        //        阅读权重：1
//        点赞权重：3
//        评论权重：5
//        收藏权重：8
        Integer score = 0;
        if (apArticle.getViews()!=null){
            score =+ apArticle.getViews();
        }
        if (apArticle.getLikes() != null) {
            score =+ apArticle.getLikes()*3;
        }
        if (apArticle.getComment() != null){
            score =+ apArticle.getComment()*5;
        }
        if (apArticle.getCollection()!= null){
            score =+ apArticle.getCollection()*8;
        }
        return score;
    }








}
